package com.iot.inter.interfaceprocessing.newinterface;

import com.iot.inter.entity.common.OrderPaymentManager;
import com.iot.inter.entity.newpojo.Constant;
import com.iot.inter.entity.newpojo.TurntoInterfaceUtils;
import com.iot.inter.entity.newpojo.request.RequestBody.ExtraPackageOrderRequestBody;
import com.iot.inter.entity.newpojo.request.RequestBody.ExtraPackagesCancelRequestBody;
import com.iot.inter.entity.newpojo.request.RequestBody.ExtraPackagesSyncRequestBody;
import com.iot.inter.entity.newpojo.request.RequestHeader;
import com.iot.inter.entity.newpojo.request.subRequest.ExtraPackageOrderRequest;
import com.iot.inter.entity.newpojo.request.subRequest.ExtraPackagesCancelRequest;
import com.iot.inter.entity.newpojo.request.subRequest.ExtraPackagesSyncRequest;
import com.iot.inter.entity.newpojo.response.subResponse.ExtraPackageOrderResponse;
import com.iot.inter.entity.newpojo.response.subResponse.ExtraPackagesCancelResponse;
import com.iot.inter.entity.newpojo.response.subResponse.ExtraPackagesSyncResponse;
import com.iot.inter.utils.HttpClientTool;
import com.iot.inter.utils.JaxbUtil;
import org.apache.log4j.Logger;

/**
 * 操作加油包相关内容：
 * 1，获取加油包流量信息
 * 2，订购加油包
 * 3，取消加油包
 *
 * @author lm
 * @date 2019/8/5
 */
public class TurnToExtraService {

    private static Logger logger = Logger.getLogger(TurnToExtraService.class);

    /**
     * 去北京做：3.19获取加油包流量信息:2.0接口
     * 接入方可以通过调用该接口，可以获取允许订购的加油包流量信息；
     *
     * @author lm
     * @date 2019/8/5
     */
    public OrderPaymentManager extraSync(String secret, String partnerCode) {

        try {

            ExtraPackagesSyncRequestBody body = new ExtraPackagesSyncRequestBody();
            // 请求参数拼接
            ExtraPackagesSyncRequest request = new ExtraPackagesSyncRequest();
            TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
            //拼装头信息
            RequestHeader header = utils.getRequestHeader(Constant.cmd_extraPackagesSync, partnerCode);
            request.setHeader(header);
            request.setBody(body);
            String sign = utils.getSign(header, body, body.getClass(), secret);
            request.setSign(sign);

            // 将参数转换成XML格式
            String sRequest = new JaxbUtil(ExtraPackagesSyncRequest.class).toXml(request, "utf-8");
            logger.info("【获取加油包流量信息】xml请求：" + sRequest);
            // 发送post请求，并接收返回结果
            String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
            logger.info("url=" + Constant.BASE_URL + ";接口名：" + Constant.cmd_extraPackagesSync);

            logger.info("【获取加油包流量信息】xml响应：" + result);

            ExtraPackagesSyncResponse responseObj = new JaxbUtil(ExtraPackagesSyncResponse.class).fromXml(result, "utf-8");
            String retCode = responseObj.getHeader().getResultCode();
            String resultInfo = responseObj.getHeader().getResultInfo();

            if (responseObj != null && true == Constant.RET_CODE_0000.equalsIgnoreCase(retCode)) {

                OrderPaymentManager user = new OrderPaymentManager();
                user.setFlowList(responseObj.getBody().getFlowList());
                user.setRetCode(retCode);
                user.setResultInfo(resultInfo);
                return user;

            } else {

                OrderPaymentManager user = new OrderPaymentManager();
                user.setRetCode(retCode);
                user.setResultInfo(resultInfo);
                return user;
            }

        } catch (Exception e) {
            OrderPaymentManager user = new OrderPaymentManager();
            user.setRetCode("9999");
            user.setResultInfo("出现了异常！");
            return user;

        }


    }

    /**
     * 去北京做：3.13	订购加油包:2.0接口
     * 接入方可以通过调用该接口，可以为某个订单订购加油包；
     * 订购加油包时，首先判断订单关联的套餐是否是预付费的，而且订单还没有过期。只有对于预付费的且未过期的订单，才允许订购加油包。
     * 如果套餐是后付费的：不允许订购加油包。
     *
     * @author lm
     * @date 2019/8/5
     */
    public OrderPaymentManager extraPackageOrder(String orderId, String refpackageFlow, String isOpen, String secret, String partnerCode) {

        try {
            ExtraPackageOrderRequestBody body = new ExtraPackageOrderRequestBody();
            body.setOrderId(orderId);
            body.setRefpackageFlow(refpackageFlow);
            body.setIsOpen(isOpen);
            // 请求参数拼接
            ExtraPackageOrderRequest request = new ExtraPackageOrderRequest();
            TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
            //拼装头信息
            RequestHeader header = utils.getRequestHeader(Constant.cmd_extraPackageOrder, partnerCode);
            request.setHeader(header);
            request.setBody(body);
            String sign = utils.getSign(header, body, body.getClass(), secret);
            request.setSign(sign);

            // 将参数转换成XML格式
            String sRequest = new JaxbUtil(ExtraPackageOrderRequest.class).toXml(request, "utf-8");
            logger.info("【订购加油包】xml请求：" + sRequest);
            // 发送post请求，并接收返回结果
            String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
            logger.info("url=" + Constant.BASE_URL + ";接口名：" + Constant.cmd_extraPackageOrder);

            logger.info("【订购加油包】xml响应：" + result);

            ExtraPackageOrderResponse responseObj = new JaxbUtil(ExtraPackageOrderResponse.class).fromXml(result, "utf-8");
            String retCode = responseObj.getHeader().getResultCode();
            String resultInfo = responseObj.getHeader().getResultInfo();


            OrderPaymentManager user = new OrderPaymentManager();
            user.setRetCode(retCode);
            user.setResultInfo(resultInfo);
            return user;

        } catch (Exception e) {
            OrderPaymentManager user = new OrderPaymentManager();
            user.setRetCode("9999");
            user.setResultInfo("出现了异常！");
            return user;

        }
    }

    /**
     * 去北京做：3.14 取消加油包:2.0接口
     * 接入方可以通过调用该接口，取消该订单订购的加油包。
     *
     * @author lm
     * @date 2019/8/5
     */
    public OrderPaymentManager extraPackageCancel(String orderId, String refOrderId, String secret, String partnerCode) {

        try {

            ExtraPackagesCancelRequestBody body = new ExtraPackagesCancelRequestBody();
            body.setOrderId(orderId);
            body.setRefOrderId(refOrderId);
            // 请求参数拼接
            ExtraPackagesCancelRequest request = new ExtraPackagesCancelRequest();
            TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
            //拼装头信息
            RequestHeader header = utils.getRequestHeader(Constant.cmd_extraPackageCancel, partnerCode);
            request.setHeader(header);
            request.setBody(body);
            String sign = utils.getSign(header, body, body.getClass(), secret);
            request.setSign(sign);

            // 将参数转换成XML格式
            String sRequest = new JaxbUtil(ExtraPackagesCancelRequest.class).toXml(request, "utf-8");
            logger.info("【取消加油包】xml请求：" + sRequest);
            // 发送post请求，并接收返回结果
            String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
            logger.info("url=" + Constant.BASE_URL + ";接口名：" + Constant.cmd_extraPackageCancel);

            logger.info("【取消加油包】xml响应：" + result);

            ExtraPackagesCancelResponse responseObj = new JaxbUtil(ExtraPackagesCancelResponse.class).fromXml(result, "utf-8");
            String retCode = responseObj.getHeader().getResultCode();
            String resultInfo = responseObj.getHeader().getResultInfo();

            OrderPaymentManager user = new OrderPaymentManager();
            user.setRetCode(retCode);
            user.setResultInfo(resultInfo);
            return user;

        } catch (Exception e) {
            OrderPaymentManager user = new OrderPaymentManager();
            user.setRetCode("9999");
            user.setResultInfo("出现了异常！");
            return user;

        }


    }
}
